#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include "lianjie.h"
typedef struct kehu {
    int num;        //连接序
    char name[10];  //客户名
    long len;       //消息长度
    char buf[128];  //存放消息
} kehu;
void *getqq(void *arg);
int main(int argv, char **argc) {
    kehu mm;
    memset(&mm, 0, sizeof(kehu));
    pthread_t t1_getqq;
    int fd = lianjie(argc[1], argc[2]);
    if (fd == -1) {
        return -1;
    }
    printf("请输入用户名:\n>");
    gets(mm.name);
    write(fd, mm.name, sizeof(mm.name));
    int jc1 = pthread_create(&t1_getqq, NULL, getqq, (void *)&fd);
    printf("消息输入格式：(对象：消息内容)\n");
    while (1) {
        sleep(1);
        printf(">");
        gets(mm.buf);
        if (strstr(mm.buf, ":") != NULL) {
            if (strstr(mm.buf, "nb666:") != NULL) {
                if (strstr(mm.buf, "quit") != NULL) {
                    write(fd, mm.buf, sizeof(mm.buf));
                    close(fd);
                    return 0;
                } else if (strstr(mm.buf, "history") != NULL) {
                    //write(fd, mm.buf, sizeof(mm.buf));
                }else{
                    printf("您输入的命令有误！\n");
                }
            }else{
                    write(fd, mm.buf, sizeof(mm.buf));
            }
        }else{
            memset(mm.buf,0,sizeof(mm.buf));
        }
    }
    return 0;
}
void *getqq(void *arg) {
    int fd = *(int *)arg;
    char buf[128] = {0};
    while (1) {
        read(fd, buf, sizeof(buf));
        if (strstr(buf, ":") != NULL) {
            printf(">%s\n", buf);
        } else {
            printf("当前存在的客户：");
            printf("%s\n", buf);
        }
    }
}
